'use strict';

angular.module('app')
    .controller('updateController', ['$scope', '$rootScope', '$state', '$timeout', 'toaster',
        function ($scope, $rootScope, $state, $timeout, toaster) {
            var title = "";
            if ($state.includes('**.media.update')) {
                title = "编辑媒体";
                var id = $state.params.id;
                activate(id);
            } else if ($state.includes('**.media.create')) {
                title = "添加媒体";
                $scope.record = {};
                $scope.record.enable = 'Y';
                $scope.record.flowType = '1';
                $scope.record.symbol = "*";
                setTimeout(function () {
                    !$rootScope.$$phase && $scope.$apply();
                }, 300);
            }
            $scope.title = $rootScope.title = title;
            $scope.loading = true;
            $scope.submit = function () {
                var m = $scope.record;
                if (m) {
                    if (!/^\d*\.?\d+$/.test($('#widthSize').val())) {
                        $scope.alertModal({'content': '尺寸宽度必须为数字'});
                        return;
                    }
                    if (!/^\d*\.?\d+$/.test($('#heightSize').val())) {
                        $scope.alertModal({'content': '尺寸高度必须为数字'});
                        return;
                    }
                    var Expression = /http(s)?:\/\/([\w-]+\.)+[\w-]+(\/[\w- .\/?%&=]*)?/
                    var objExp = new RegExp(Expression);
                    if ($('#mediaLink').val() != '' && objExp.test($('#mediaLink').val()) == false) {
                        $scope.alertModal({'content': '请输入正确的媒体网址'});
                        return;
                    }
                    if ($('#slotLink').val() != '' && objExp.test($('#slotLink').val()) == false) {
                        $scope.alertModal({'content': '请输入正确的广告位网址'});
                        return;
                    }
                    $scope.record.slotSize = $scope.record.widthSize + $scope.record.symbol + $scope.record.heightSize;
                    $.ajax({
                        url: $scope.record.id ? $scope.app.project_name + '/media/update' : $scope.app.project_name + '/media/add',
                        data: $scope.record
                    }).then(callback);
                }
                function callback(result) {
                    if (result.httpCode == 200) {//成功
                        var msg = result.errorMsg;
                        if (msg != '' && msg != null) {
                            $scope.alertModal({"content": msg});
                        }else{
                            toaster.clear('*');
                            $scope.alertModal({"content": "操作成功"});
                            $timeout(function () {
                                $state.go('main.toolChest.media.list');
                            }, 2000);
                        }
                    } else {
                        toaster.clear('*');
                        toaster.pop('error', '', result.msg);
                        $scope.isDisabled = false;
                    }
                }
            }

            // 初始化页面
            function activate(id) {
                $scope.loading = true;
                $.ajax({
                    url: $scope.app.project_name + '/media/detail',
                    data: {'id': id}
                }).then(function (result) {
                    $scope.loading = false;
                    if (result.httpCode == 200) {
                        $scope.record = result.data;
                        var slotSize = $scope.record.slotSize;
                        var array = [];
                        if (slotSize.indexOf("*") >= 0) {
                            array = slotSize.split("*");
                            $scope.record.symbol = "*";
                        }else{
                            array = slotSize.split("x");
                            $scope.record.symbol = "x";
                        }
                        $scope.record.widthSize = array[0];
                        $scope.record.heightSize = array[1];
                    } else {
                        $scope.msg = result.msg;
                    }
                    $scope.$apply();
                });
            }
        }]);